package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.AdvertisementModel;
import com.app.model.AdvertisementSonModel;
import com.app.service.AdminUserService;
import com.app.service.AdvertisementService;
import com.app.service.AdvertisementSonService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.AdvertisementVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Controller
public class AdvertisementAdminController {
    @Autowired
    AdvertisementService advertisementService;
    @Autowired
    AdvertisementSonService advertisementSonService;
    @Autowired
    AdminUserService adminUserService;


    /**
     * 后台查询广告列表
     */
    @GetMapping("/getAdminAdvertisementList")
    @ResponseBody
    public Result<Object> getAdminAdvertisementList(HttpServletRequest request,Integer userType,Integer seat,Integer page,Integer limit) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        AdvertisementModel model = new AdvertisementModel();
        model.setUserType(userType);
        model.setSeat(seat);
        model.setPage(page);
        model.setLimit(limit);
        Page<AdvertisementModel> modelPage = advertisementService.getPageByModel(model);
        AdvertisementVo vo = new AdvertisementVo();
        vo.setList(modelPage.getRecords());
        for (AdvertisementModel advertisementModel:vo.getList()) {
            if(advertisementModel.getUserType()==1)
            {
                advertisementModel.setUserTypeText("个人");
            }else{
                advertisementModel.setUserTypeText("企业");
            }
            if(advertisementModel.getSeat()==1)
            {
                advertisementModel.setSeatText("小程序首页");
            }else if(advertisementModel.getSeat()==2)
            {
                advertisementModel.setSeatText("PC招聘首页位置1");
            }else if(advertisementModel.getSeat()==3)
            {
                advertisementModel.setSeatText("PC招聘首页位置2");
            }else if(advertisementModel.getSeat()==4)
            {
                advertisementModel.setSeatText("小程序首页弹出广告");
            }
        }
        vo.setCount((int)modelPage.getTotal());
        return Result.success(vo);
    }

    /**
     * 编辑广告
     */
    @PostMapping("/editAdvertisement")
    public Result<Object> editAdvertisement(HttpServletRequest request,
                                            @RequestBody AdvertisementModel model) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        if(model.getId()!=null&&model.getId()>0)
        {
            advertisementService.update(model);
        }else {
            advertisementService.insert(model);
        }
        advertisementSonService.delete(model.getId());
        for (int i = 0; i < model.getImgUrlList().length; i++) {
            AdvertisementSonModel advertisementSonModel = new AdvertisementSonModel();
            advertisementSonModel.setImgUrl(model.getImgUrlList()[i]);
            advertisementSonService.insert(advertisementSonModel);
        }
        return Result.success("操作成功");
    }

    /**
     * 根据广告ID查询
     */
    @GetMapping("/getAdvertisementById")
    @ResponseBody
    public Result<Object> getAdvertisementById(HttpServletRequest request,
                                            Integer id) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        AdvertisementModel model = advertisementService.getModelById(id);
        return Result.success(model);
    }

    /**
     * 根据广告ID删除
     */
    @PostMapping("/deleteAdvertisementById")
    @ResponseBody
    public Result<Object> deleteAdvertisementById(HttpServletRequest request,
                                               Integer id) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }

        advertisementService.delete(id);
        return Result.success("操作成功");
    }
}
